<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 */
namespace  app\home\controller;

use  Mscore\Core\Controller;
use  app\home\model\Advert;
use  app\home\model\Menu;
use  app\home\model\Basics;
use  app\home\model\Article;
use  app\home\model\Single;
use  app\home\model\Partner;
use  app\home\model\Record;
use  app\home\model\Product;
use  Mscore\Core\Util\Util;
use  Mscore\Core\Lang;

class WebController extends Controller{

    public $menu;
    public $rmenu;
    public $config;
    public $auth;
    public $token;
    public $template;
    public $custer;
    public $file;
    // public $acode;

    public function __construct()  
    {    
        $this->config = (new Basics())->getconfig('id=1');
        $this->template = $this->config['site_template'];
        if((!$this->config['site_status'])){
            Header("Location:/home/error/close/");
        }
        $this->languages = (new Basics())->getLang();
        if(!isset($_COOKIE['language'])&&empty($_COOKIE['language'])){
            $this->language = $this->languages['default'];
        }else{
            $this->language = $_COOKIE['language'];
        }
        // Lang::init($this->language);
        //菜單
        $this->menu = (new Menu())->menuList('status=1 and special=0',$this->language);
        $this->rmenu = (new Menu())->menuList('status=1 and special=1',$this->language);
        // $this->acode = array_column($this->menu, 'acode');
        //客服
        $this->custer=(new Basics())->getcuster('type=3',$this->language);
        $this->file=['url'=>config('file.baseurl')];  
    } 
        
    /**
     * 首页默认方法
     * @param array $index 
    */
    public function index(){
        
        //获取访问量
        (new Record())->countnum('index');
        //首页广告
        $indexad=(new Advert())->getAdClass(' advcode="index" and status=1','id,icon');
        if($indexad){
          $ad = (new Advert())->advertList('cid='.$indexad['id'].' and status=1',$this->language);
          foreach ($ad as $key => $value) {
            if(strpos($value['imgurl'], ',')!== false){
              $ad[$key]['imgurl']=explode(',', $value['imgurl'])[0];
              $ad[$key]['mimgurl']=explode(',', $value['imgurl'])[1];
            }
          }
        }else{
          $ad=[];
        }
        // print_r($ad);die;

        //单项内容
        $singlecate=(new Single())->singlecate('is_index=2 and status=1 and sid<1',$this->language);
        if($singlecate){
          foreach ($singlecate as $key => $value) {
                switch ($value['type']) {
                    case 1:
                        switch ($value['cssid']) {
                            case 1: //取文件栏目
                                $singlecate[$key]['single'] = (new Single())->concate('acode="'.$value['acode'].'" and status=1 ',$this->language); 
                                break;
                            case 2: //取文件栏目
                                $singlecate[$key]['single'] = (new Single())->concate('acode="'.$value['acode'].'" and status=1 ',$this->language); 
                                break;
                            case 3: //取本身栏目
                                $singlecate[$key]['single'] = (new Single())->singlecate('sid="'.$value['id'].'" and status=1 ',$this->language); 
                                break;
                            case 4: //取本身栏目
                                $singlecate[$key]['single'] =[]; 
                                break;
                            default:
                                $singlecate[$key]['single']=[];
                                break;
                        }
                        break;
                    case 2:
                        $singlecate[$key]['single'] = (new Single())->artcate(' pid='.$value['pid'].' and status=1 and is_index=2 and pid>0',$this->language);
                        switch ($value['cssid']) { 
                            case 1: //取文章栏目
                                $singlecate[$key]['list'] = (new Single())->articlelist('acode="'.$value['acode'].'" and status=1 and is_index=2',$this->language);
                                break;
                            case 2: //取文章栏目
                                foreach ($singlecate[$key]['single'] as $k => $val) {
                                    $singlecate[$key]['single']['list'] = (new Single())->articlelist('cid="'.$val['id'].'" and status=1 and is_index=2',$this->language);
                                }
                                break;
                            case 3: //取本身栏目
                                $singlecate[$key]['single'] = (new Single())->singlecate('sid="'.$value['id'].'" and status=1 ',$this->language); 
                                break;
                            default:
                                $singlecate[$key]['single']=[];
                                break;
                        }
                        break;
                    default:
                        $singlecate[$key]['single'] = [];
                        break;
                }
            }
        }
        //首页广告
        $this->assign('indexad',$indexad);
        $this->assign('ad',$ad);
        
        $this->assign('auth',$this->auth);
        $this->assign('usercode',get('usercode'));

//        echo "<pre>";
//        print_r($singlecate);
//        echo "<pre>";
        $this->assign('singlecate',$singlecate);
        $this->assign('custer',$this->custer);
        unset($this->languages['default']);
        $this->assign('languages',$this->languages);
        $this->assign('language',$this->language);
        $this->assign('lang',config($this->language,'/config/lang')['page']['table']);

        $this->assign('menu',$this->menu);
        $this->assign('rmenu',$this->rmenu);
        $this->assign('config',$this->config);
        $this->assign('file',$this->file);
        $this->display("web".$this->template."\index.html");
    }

    /**
     * 关闭页面
     * @param array $error 
    */
    public function error(){

        $this->display("error.html");
    }

    /**
     * 设置语言
     * @param array $login 
     * @param array $where 
    */
    public function setlanguage(){
        $lang = post('lang',false); $lang = isset($lang)?$lang:'default';
        if(isset($_COOKIE['language'])){
            unset($_COOKIE['language']);
        }
        if (!setcookie("language", $lang, time() + 365*24*60*60,'/')){
            $info=['code'=>100,'info'=>"set failed"];
            $this->echoJson($info);
        }
        $info=['code'=>200];
        $this->echoJson($info);
    }

    /**
     * 單項頁面
     * @param array $e-commerc 
    */
    public function single(){
        // if(($this->auth!=2)){
        //     Header("Location:/home/web/index");
        // }
        //广告
        $indexad=(new Advert())->getAdClass(' advcode="'.get('acode').'" and status=1','id,icon');
        if($indexad){
          $ad = (new Advert())->advert('cid='.$indexad['id'].' and status=1',$this->language);
        }else{
          $ad='';
        }

        $singleinfo=(new Single())->single('status=1 and acode="'.get('acode').'"',$this->language);
        $itemList=(new Single())->concate('status=1 and id="'.get('id').'"',$this->language);
        if ($itemList){
            $itemInfo=$itemList[0];
        }else{
            $itemInfo=[];
        }

        //内容
        $this->assign('single',$singleinfo);
        $this->assign('iteminfo',$itemInfo);

        $this->assign('acode',get('acode'));
        //首页广告
        $this->assign('indexad',$indexad);
        $this->assign('ad',$ad);
        unset($this->languages['default']);
        //其他
        $this->assign('languages',$this->languages);
        $this->assign('language',$this->language);
        $this->assign('custer',$this->custer);
        $this->assign('menu',$this->menu);
        $this->assign('rmenu',$this->rmenu);
        $this->assign('config',$this->config);
        $this->assign('file',$this->file);
        $this->display("web".$this->template."\single.html");
    }

    /**
     * 文章類別頁面
     * @param array $arcate 
    */
    public function arcate(){
        // if(($this->auth!=2)){
        //     Header("Location:/home/web/index");
        // }
        //广告
        $indexad=(new Advert())->getAdClass(' advcode="'.get('acode').'" and status=1','id,icon');
        if($indexad){
          $ad = (new Advert())->advert('cid='.$indexad['id'].' and status=1',$this->language);
        }else{
          $ad='';
        }

        //获取栏目
        if(get('acode')){
            $cate=(new Article())->getcateinfo('status=1 and pid=0 and acode="'.get('acode').'"',$this->language);
            //获取列表
            if(!empty($cate)){
            	$page = !empty(get('page'))?get('page'):1;
            	$limit = !empty(get('limit'))?get('limit'):10;
            	$cate['articlelist']['datalist']=(new Article())->getcatelist('pid= '.$cate['id'].' and status=1',$this->language);
            }
        }elseif(get('id')){
            $cate=(new Article())->getcateinfo('status=1 and id='.get('id'),$this->language);
            if(!empty($cate)){
                $subcate=(new Article())->getcateinfo('status=1 and id='.$cate['pid'],$this->language);
                $cate['title']=isset($subcate['title'])?$subcate['title']:$cate['title'];
                unset($subcate);
            }
            //获取文章
            $page = !empty(get('page'))?get('page'):1;
            $limit = !empty(get('limit'))?get('limit'):10;
            $cate['articlelist']=(new Article())->articlelist('cid= '.$cate['id'].' and status=1',$this->language,$page,$limit);
        }

        $this->assign('cate',$cate);

        //首页广告
        $this->assign('indexad',$indexad);
        $this->assign('ad',$ad);
        $this->assign('acode',get('acode'));

        $this->assign('languages',$this->languages);
        $this->assign('language',$this->language);
        $this->assign('menu',$this->menu);
        $this->assign('rmenu',$this->rmenu);
        $this->assign('custer',$this->custer);
        $this->assign('config',$this->config);
        $this->assign('file',$this->file);
        $this->display("web".$this->template."\articlecate.html");
    }

    /**
     * 内容詳情頁面
     * @param array $article
    */
    public function article(){
        // if(($this->auth!=2)){
        //     Header("Location:/home/web/index");
        // }
        $article=(new Article())->article('id='.get('id'),$this->language);

        // $article_pre=(new Article())->article('id<'.get('id'), $this->language ,'id,title');
        // $article_next=(new Article())->article('id>'.get('id'), $this->language ,'id,title');

        //栏目
        $cate=(new Article())->getcateinfo('id='.$article['cid'], $this->language);
        if(empty($cate)){
        	$cate=(new Article())->getcateinfo('acode="'.$article['acode'].'"', $this->language);
        }
        $this->assign('acode',$article['acode']);

        //广告
        $indexad=(new Advert())->getAdClass(' advcode="'.$article['acode'].'" and status=1','id,icon');
        if($indexad){
          $ad = (new Advert())->advertList('cid='.$indexad['id'].' and status=1',$this->language);
        }else{
          $ad='';
        }

        $this->assign('article',$article);
        // $this->assign('article_pre',$article_pre);
        // $this->assign('article_next',$article_next);

        $this->assign('cate',$cate);

        //首页广告
        $this->assign('indexad',$indexad);
        $this->assign('ad',$ad);
        unset($this->languages['default']);
        $this->assign('languages',$this->languages);
        $this->assign('language',$this->language);

        $this->assign('menu',$this->menu);
        $this->assign('rmenu',$this->rmenu);
        $this->assign('custer',$this->custer);
        $this->assign('config',$this->config);
        $this->assign('file',$this->file);
        $this->display("web".$this->template."\article.html");
    }

}